import time

from selenium.webdriver.common.by import By
import unittest
from quote.base.browser_operation import BrowserOP
from quote.base.use_browser import UseBrowser
from quote.util.excel_op import ExcelOp
from quote.util.yamlOp import YamlOp
from quote.webpage.user_manager.login_page import LoginPage


class CustomerPage:
    def __init__(self):
        # self.x =UseBrowser()
        # self.y = BrowserOP(UseBrowser.driver)
        self.z =  LoginPage()
        self.p =  YamlOp()
        self.exl = ExcelOp()
        self.exl.get_sheet('客户用例参数')


    def customer_add(self,customerNO,customerName,phone,address,man,info):  #获取添加的信息
        self.z.login(self.p.get_locator('LoginInfo','username'),self.p.get_locator('LoginInfo','password'))
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_Links'))
        self.z.a.find_element(By.XPATH,self.p.get_locator('CustomerPage','add_1_xpath')).click()
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_main'))
        self.z.a.find_element(By.XPATH,self.p.get_locator('CustomerPage','add_2_xpath')).click()
        self.z.a.change_window('新增客户信息')
        # 输入客户编号
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','Number')).send_keys(customerNO)
        # 输入客户姓名
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','Name')).send_keys(customerName)
        # 输入客户联系电话
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','phone')).send_keys(phone)
        # 输入客户地址
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','addr')).send_keys(address)
        #联系人
        self.z.a.find_element(By.NAME,self.p.get_locator('CustomerPage','man')).send_keys(man)
        #其他信息
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','other')).send_keys(info)
        # 保存
        self.z.a.find_element(By.XPATH, self.p.get_locator('CustomerPage','add_3_xpath')).click()

    def customer_modify(self,customerName,phone,address,man,info):  #获取修改的信息
        self.z.login(self.p.get_locator('LoginInfo','username'),self.p.get_locator('LoginInfo','password'))
        self.z.a.change_window('报价管理系统')
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_Links'))
        self.z.a.find_element(By.XPATH, self.p.get_locator('CustomerPage','modify_1_xpath')).click()
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_main'))
        self.z.a.find_element(By.XPATH,self.p.get_locator('CustomerPage','modify_2_xpath')).click()
        self.z.a.change_window(self.p.get_locator('CustomerPage','window2'))
        # 输入客户姓名
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','Name')).clear()
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','Name')).send_keys(customerName)
        # 输入客户联系电话
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','phone')).clear()
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','phone')).send_keys(phone)
        # 输入客户地址
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','addr')).clear()
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','addr')).send_keys(address)
        # 联系人
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','man')).clear()
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','man')).send_keys(man)
        # 其他信息
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','other')).clear()
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage','other')).send_keys(info)
        # 保存
        self.z.a.find_element(By.XPATH, self.p.get_locator('CustomerPage','modify_3_xpath')).click()


    def customer_search(self,goal):  #获取查询的信息
        self.goal_lst = []
        self.z.login(self.p.get_locator('LoginInfo','username'),self.p.get_locator('LoginInfo','password'))
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_Links'))
        self.z.a.find_element(By.XPATH,self.p.get_locator('CustomerPage','search_1_xpath')).click()
        self.z.a.change_frame(self.p.get_locator('CustomerPage','frame_main'))
        print(self.p.get_locator('CustomerPage','Number'))
        self.z.a.find_element(By.NAME,'customerNO').send_keys(goal)
        self.z.a.find_element(By.NAME,self.p.get_locator('CustomerPage','button')).click()
        goal_num = self.z.a.find_element(By.XPATH,self.p.get_locator('CustomerPage','search_2_xpath')).text
        print(goal_num[15])
        # print(goal_num[34])
        for i in range(1,(int(goal_num[15])+1)):
            w =self.z.a.find_element(By.XPATH,'/html/body/center/form/table[1]/tbody/tr['+str(i+1)+']/td[1]').text
            self.goal_lst.append(w)
        print(self.goal_lst)
    def customer_dele(self):
        self.z.login(self.p.get_locator('LoginInfo', 'username'), self.p.get_locator('LoginInfo', 'password'))
        self.z.a.change_frame(self.p.get_locator('CustomerPage', 'frame_Links'))
        self.z.a.find_element(By.XPATH, self.p.get_locator('CustomerPage', 'modify_1_xpath')).click()
        self.z.a.change_frame(self.p.get_locator('CustomerPage', 'frame_main'))
        self.z.a.find_element(By.XPATH,self.p.get_locator('Dele','1_xpath')).click()
        self.z.a.alter_op('1')
    #老师讲
    def search_customer(self,customerNo='',customerName='',phone='',address='',man='',info=''):
        self.z.login(self.p.get_locator('LoginInfo', 'username'), self.p.get_locator('LoginInfo', 'password'))
        self.z.a.change_window('报价管理系统')
        self.z.a.change_frame(self.p.get_locator('CustomerPage', 'frame_Links'))
        self.z.a.find_element(By.XPATH, self.p.get_locator('CustomerPage', 'search_1_xpath')).click()
        self.z.a.change_frame(self.p.get_locator('CustomerPage', 'frame_main'))
        self.z.a.find_element(By.NAME, 'customerNO').send_keys(customerNo)
        # 输入客户姓名
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'Name')).send_keys(customerName)
        # 输入客户联系电话
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'phone')).send_keys(phone)
        # 输入客户地址
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'addr')).send_keys(address)
        # 联系人
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'man')).send_keys(man)
        # 其他信息
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'other')).send_keys(info)
        self.z.a.find_element(By.NAME, self.p.get_locator('CustomerPage', 'button')).click()

    #获取查询到的数据
    def get_one_info(self,start_td=1,end_td=7):
        cus_lst=[]
        for i in range(start_td,end_td):
            text = self.z.a.get_text('xpath','/html/body/center/form/table[1]/tbody/tr[2]/td[{}]'.format(i))  #format拼接
            # text = self.y.get_text('xpath', '/html/body/center/form/table[1]/tbody/tr[2]/td[' + str(i) + ']')  #字符串拼接
            cus_lst.append(text)
            cus_lst.sort()
        return cus_lst







# if __name__ == '__main__':
#     a = CustomerPage()
#     a.customer_dele()
# # #     b.login('admin','admin')
# #     a.customer_search('444')
# #     a.customer_modify('dahai','654321','neijiang','veve','111')
#     a.customer_add('16666','lin','123456','chengdu','zhangsan','无')